from rest_framework.serializers import ModelSerializer

from work.models import Label, Topic

from rest_framework import serializers


class LableSerializer(ModelSerializer):
    username = serializers.CharField(source='user.username', read_only=True)  # 新增字段username

    class Meta:
        model = Label
        fields = '__all__'  # 序列化器返回出去的是模型的所有字段
        extra_kwargs = {  # 隐藏字段,但是可以设置
            'user': {'write_only': True}
        }


class TopicSerializer(ModelSerializer):
    label_name = serializers.CharField(source='label.name', read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Topic
        exclude = ['is_delete']  #
        extra_kwargs = {
            'user': {'write_only': True},
            'label': {'write_only': True},
        }


class TopicNoAnswerSerializer(ModelSerializer):
    label_name = serializers.CharField(source='label.name', read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Topic
        exclude = ['is_delete', 'answer']
        extra_kwargs = {
            'user': {'write_only': True},
            'label': {'write_only': True}
        }


class LabelTopicSerializer(ModelSerializer):
    topic_set = TopicNoAnswerSerializer(many=True, required=False, read_only=True)  # 指向没有答案的
    
    class Meta:
        model = Label
        fields = '__all__'
